package cn.com.sparkle.paxos.event;

//import cn.com.sparkle.paxos.model.Id;
//import cn.com.sparkle.paxos.model.Value;
import cn.com.sparkle.paxos.iomodel.protobuffmessage.PaxosMessages.Id;
import cn.com.sparkle.paxos.iomodel.protobuffmessage.PaxosMessages.Value;
import cn.com.sparkle.paxos.paxosinstance.InstancePaxosInstance;

public abstract class InstancePaxosEvent implements Event {
	public final static int INSTANCE_SUCCESS = 1;
	public final static int INSTANCE_FAIL = 2;
	public final static int INSTANCE_START = 3;

	@Override
	public void event(Object... args) {
		int type = (Integer) args[0];
		switch (type) {
		case INSTANCE_SUCCESS:
			success((InstancePaxosInstance) args[1], (Value) args[2]);
			break;
		case INSTANCE_FAIL:
			fail((InstancePaxosInstance) args[1], (Id.Builder) args[2],
					(Long) args[3]);
			break;
		case INSTANCE_START:
			start((InstancePaxosInstance) args[1]);
			break;
		default:
			throw new RuntimeException("not supported argument:" + type);
		}

	}

	public final static void doFailEvent(EventsManager eventsManager,
			InstancePaxosInstance instance, Id.Builder id, long refuseId) {
		eventsManager.doEvent(InstancePaxosEvent.class, INSTANCE_FAIL,
				instance, id, refuseId);
	}

	public final static void doSuccessEvent(EventsManager eventsManager,
			InstancePaxosInstance instance, Value value) {
		eventsManager.doEvent(InstancePaxosEvent.class, INSTANCE_SUCCESS,
				instance, value);
	}

	public final static void doStartEvent(EventsManager eventsManager,
			InstancePaxosInstance instance) {
		eventsManager.doEvent(InstancePaxosEvent.class, INSTANCE_START,
				instance);
	}

	/**
	 * 
	 * @param instance
	 * @param refuseId
	 *            -1 indicates the number of active senator node is not enough
	 */
	public abstract void fail(InstancePaxosInstance instance, Id.Builder id,
			long refuseId);

	public abstract void success(InstancePaxosInstance instance, Value value);

	public abstract void start(InstancePaxosInstance instance);
}
